library(tidyverse)
library(nycflights13)
library(lvplot)
library(modelr)

smaller <- diamonds %>%
  filter(carat < 3)

ggplot(data = smaller, mapping = aes(x = carat)) +
  geom_histogram(binwidth = 0.1)

ggplot(data = smaller, mapping = aes(x = carat, colour = cut)) +
  geom_freqpoly(binwidth = 0.1)

ggplot(data = faithful, mapping = aes(x = eruptions)) +
  geom_histogram(binwidth = 0.05)

# 7.3 Variation's exercise ------
ggplot(data = diamonds) +
  geom_histogram(aes(x = x), binwidth = 5, fill = 'red') +
  geom_histogram(aes(x = y), binwidth = 5, fill = 'blue') +
  geom_histogram(aes(x = z), binwidth = 5, fill = 'green') +
  coord_cartesian(ylim = c(0, 100))

diamonds %>% filter(
  x < 1 | x > 20 |
  y < 1 | y > 20 |
  x < 1 | z > 20
)

ggplot(data = diamonds) +
  geom_histogram(aes(x = price), binwidth = 5) +
  coord_cartesian(ylim = c(0, 100), xlim = c(1455, 1545))

diamonds %>% group_by(cut_width(price, 100)) %>%
  filter(n() < 100) %>%
  ungroup() %>%
  ggplot() +
  geom_histogram(aes(x = price))
summary(diamonds)

diamonds2 <- diamonds %>%
  mutate(
    x = ifelse(x == 0 | x > 10, NA, x),
    y = ifelse(y == 0 | y > 20, NA, y),
    z = ifelse(z == 0 | z > 10, NA, z),
  )
ggplot(data = diamonds2, mapping = aes(x = x, y = y)) +
  geom_point()

ggplot(data = diamonds2, mapping = aes(x = y, y = z)) +
  geom_point()

# 7.5 Covariation ------
flights %>%
  mutate(
    cancelled = is.na(dep_time),
    sched_dep_hour = sched_dep_time %/% 100,
    sched_dep_min = sched_dep_time %% 100,
    sched_dep_time = sched_dep_hour + sched_dep_min / 60
  ) %>%
  ggplot() +
    geom_boxplot(aes(x = cancelled, y = sched_dep_time))

diamonds %>%
  mutate(
    carat_bin = cut_width(carat, 0.5, boundary = 0.25)
  ) %>%
ggplot() +
  geom_point(aes(x = carat, y = price, color = cut)) +
  facet_wrap(~ carat_bin)

diamonds %>%
  mutate(
    carat_bin = cut_width(carat, 1, boundary = 0)
  ) %>%
ggplot() +
  geom_boxplot(aes(x = cut, y = price, color = cut)) +
  facet_wrap(~ carat_bin)

ggplot(data = diamonds) +
  geom_lv(aes(x = cut, y = price, color = cut, fill = cut))

diamonds %>%
  mutate(
    carat_bin = cut_width(carat, 1, boundary = 0)
  ) %>%
ggplot() +
  geom_lv(aes(x = cut, y = price, color = cut, fill = cut)) +
  facet_wrap(~ carat_bin)

diamonds %>%
  mutate(
    carat_bin = cut_width(carat, 1, boundary = 0)
  ) %>%
ggplot() +
  geom_violin(aes(x = cut, y = price, color = cut, fill = cut)) +
  facet_wrap(~ carat_bin)

diamonds %>%
  count(color, cut) %>%
  ggplot(mapping = aes(x = color, y = cut)) +
  geom_tile(mapping = aes(fill = n))

ggplot(data = diamonds) +
  geom_point(mapping = aes(x = carat, y = price), alpha = 1 / 100)

ggplot(data = smaller) +
  geom_bin2d(mapping = aes(x = carat, y = price))

# install.packages("hexbin")
ggplot(data = smaller) +
  geom_hex(mapping = aes(x = carat, y = price))

ggplot(data = smaller, mapping = aes(x = cut, y = price)) +
  geom_boxplot() +
  facet_wrap(~ cut_width(carat, 0.5, boundary = 0))

mod <- lm(log(price) ~ log(carat), data = diamonds)

diamonds2 <- diamonds %>%
  add_residuals(mod) %>%
  mutate(resid = exp(resid))

ggplot(data = diamonds2) +
  geom_point(mapping = aes(x = carat, y = resid))

ggplot(data = diamonds2) +
  geom_boxplot(mapping = aes(x = cut, y = resid))
